#include "BankSystem.hh"
#include "Account.hh"
#include "ReportGenerator.hh"
#include <iostream>
#include <limits>

using namespace std;

BankSystem::BankSystem() {}

void BankSystem::registerCustomer(const string& name) {
    cout << "\n===== Register New Customer =====\n";
    cout << "Enter customer name: " << name << "\n"; // 可选：你已在 main menu 获取输入

    auto customer = make_shared<Customer>(name);
    customers.push_back(customer);

    cout << "\nCustomer \"" << name << "\" successfully registered!\n";
    cout << "Two bank accounts have been created:\n";

    const auto& accounts = customer->getAccounts();
    for (const auto& acc : accounts) {
        cout << "- " << acc->getAccountType() << ": " << acc->getID() << "\n";
    }

    cout << "\nPress Enter to continue...";
    cin.ignore();
    cin.get();
}

shared_ptr<Customer> BankSystem::findCustomer(const string& name) {
    for (auto& cust : customers) {
        if (cust->getName() == name) {
            return cust;
        }
    }
    return nullptr;
}

void BankSystem::showAllCustomers() const {
    cout << "\n=== All Customers ===\n";
    if (customers.empty()) {
        cout << "No customers found.\n";
    } else {
        for (const auto& customer : customers) {
            cout << "- " << customer->getName() << "\n";
        }
    }
}

void BankSystem::showMainMenu() {
    int choice;
    string name;
    shared_ptr<Customer> customer;

    while (true) {
        cout << "\n===== Mini Banking System =====\n";
        cout << "1. Register new customer\n";
        cout << "2. Access customer accounts\n";
        cout << "3. Display all customers\n";
        cout << "4. Transaction reports\n";
        cout << "5. Exit\n";
        cout << "Please choose (1-5): ";

        if (!(cin >> choice)) {
            cout << "Invalid input. Please enter a number between 1 and 5.\n";
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            continue;
        }

        cin.ignore();  // 清除换行符

        switch (choice) {
        case 1:
            cout << "\n===== Register New Customer =====\n";
            cout << "Enter customer name: ";
            getline(cin, name);
            registerCustomer(name);
            break;

        case 2:
            cout << "\n===== Access Customer Accounts =====\n";
            cout << "Enter customer name: ";
            getline(cin, name);
            customer = findCustomer(name);
            if (customer) {
                cout << "===== Customer: " << customer->getName() << " =====\n";
                customer->showAccounts();
            } else {
                cout << "Customer not found.\n";
            }
            break;

        case 3:
            showAllCustomers();
            break;

        case 4:
            cout << "Generate global transaction report (Y/N)? ";
            char reportChoice;
            cin >> reportChoice;
            if (reportChoice == 'Y' || reportChoice == 'y') {
                generateGlobalTransactionReport();
            }
            cin.ignore();
            break;

        case 5:
            cout << "Exiting banking system...\n";
            return;

        default:
            cout << "Invalid choice. Please try again.\n";
        }
    }
}

void BankSystem::generateGlobalTransactionReport() const {
    ReportGenerator::generateGlobalTransactionReport(customers);
}

void BankSystem::generateCustomerReport(shared_ptr<Customer> customer) const {
    ReportGenerator::generateCustomerTransactionReport(customer);
}

void BankSystem::generateAccountReport(Account* account) const {
    ReportGenerator::generateAccountTransactionReport(account);
}
